﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AgentSystem.AgentCreator.Util;
using System.CodeDom;

namespace AgentSystem.AgentCreator.Sbpel.Activities
{
    /// <summary>
    /// Activity class for representing Variables
    /// </summary>
    public class VariableActivity : AbstractActivity
    {
        /// <value>
        /// The name of the variable.
        /// </value>
        public string VariableName { get; private set; }

        /// <value>
        /// The type of the variable.
        /// </value>
        public string VariableType { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="VariableActivity"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="type">The type.</param>
        public VariableActivity(AbstractActivity parent, string name, string type) : base(parent)
        {
            VariableName = name;
            VariableType = type;
        }

        public override CodeObject[] GetCodeDom()
        {
            try
            {
                Type selectedType = Type.GetType(VariableType, true, false);
                CodeMemberField field = new CodeMemberField(selectedType, VariableName);
                field.Attributes = MemberAttributes.Public;

                return new CodeObject[] { field }; 
            }
            catch(Exception exception)
            {
                var errorMessage = string.Format("An error has been thrown while trying to create variable \"{0}\". See inner exception.", Name);
                throw new SbperException(errorMessage, exception);
            }
        }
    }
}
